/**
 * @file order.js
 * @author swan
 */
const app = getApp()
var orderData = require('../../data/goods-data.js')
Page({
    data: {
        status: -1,
        host: getApp().getUrl(),
        page: 1,
        isloading: true,
        isReachBottom: false,
        orderLen: true,
        totalPage: null,
        orderList: []
    },
    onLoad: function () {
        let openID = getApp().getOpenID(),
            token = getApp().getToken();
        if (!openID || !token) {
            // 没有openID，说明只是手百登录，但是还没有与三方服务器关联，需要先获取openID
            getApp().loginThenDoEvent(getApp().forceGetUserInfoThenDecrypt);
            return false;
        }
        console.log(this.data.status)
        this.orderList(this.data.page, openID, token, this.data.status);
    },
    onPullDownRefresh(e) {
        var that = this;
        this.setData({
            page: 1,
            isloading: false,
            isReachBottom: false,
            orderList: []
        })
        that.onLoad();    //刷新页面
        setTimeout(function () {
            swan.stopPullDownRefresh();    //停止下拉动作
        }, 1000)

    },
    onReachBottom(e) {
        var page = this.data.page;
        var totalPage = this.data.totalPage + 1;
        page++;
        this.setData({
            isReachBottom: true
        });
        if (page > totalPage) {
            this.setData({
                isloading: false,
            });
            return false;
        }
        this.setData({
            page: page
        })
        let openID = getApp().getOpenID(),
            token = getApp().getToken();
        this.orderList(this.data.page, openID, token, this.data.status);
    },
    orderList(page, openID, token, status) {
        swan.showToast({
            title: '正在加载...',
            icon: 'loading',
            duration: 10000,
            mask: true
        });
        var that = this;
        swan.request({
            url: getApp().getUrl('/v1/order/order-list'),
            data: {
                open_id: openID,
                status: status,
                page: page
            },
            header: {
                'Authorization': 'Bearer ' + token,
            },
            method: 'POST',
            success: res => {
                swan.hideToast();
                let data = res.data;
                console.log(data);
                if (data.status != 1) {
                    swan.showToast({
                        title: data.message,
                        icon: 'none'
                    });
                    if (data.status == 1004) {
                        getApp().loginThenDoEvent(getApp().forceGetUserInfoThenDecrypt);
                    }
                    return false;
                }

                var newsArr = that.data.orderList;
                for (var i = 0; i < data.data.data.length; i++) {
                    newsArr.push(data.data.data[i])
                }
                let orderLen = newsArr.length == 0 ? false : true;
                that.setData({
                    orderList: newsArr,
                    isloading: true,
                    orderLen: orderLen,
                    totalPage: data.data.totalPage
                });
            },
            fail: err => {
                swan.hideToast();
                swan.showToast({
                    title: '订单展示失败',
                    icon: 'none'
                });
            }
        });
    },
    onOrderTap(event) {
        var status = event.currentTarget.dataset.status;
        console.log(status);
        this.setData({
            status: status,
            page: 1,
            isloading: false,
            isReachBottom: false,
            orderList: []
        })
        let openID = getApp().getOpenID(),
            token = getApp().getToken();
        this.orderList(this.data.page, openID, token, this.data.status);
    },
    onGoodsTap: function (event) {
        var goodsId = event.currentTarget.dataset.goodsId;
        console.log(goodsId)
        swan.navigateTo({
            url: "../goods/goods-detail/goods-detail?id=" + goodsId
        })
    },
    onOrderInfoTap(event) {
        var orderNo = event.currentTarget.dataset.orderNo;
        swan.navigateTo({
            url: "../order/order-detail/order-detail?id=" + orderNo
        })
    },
    showModalNotitle(e) {
        swan.showModal({
            content: '添加客服QQ:800077631',
            showCancel: false
        });
    },
});
